package trocabi.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import trocabi.TROCABI;
import java.awt.Dimension;

public class Accueil extends JFrame implements ActionListener, MouseListener {

    // Déclaration variables
    private static JPanel pContentPanel;
    private static JLabel lTitre;
    private static JPanel pCenter;
    private static JButton jbDepot;
    private static JPanel pDepot;
    private static JButton jbFacturation;
    private static JPanel pFacturation;
    private static JButton jbInventaire;
    private static JPanel pInventaire;
    private static JButton jbVente;
    private static JPanel pVente;

    public Accueil() {
        initComponents();
    }

    private void initComponents() {
        this.setTitle("Accueil");
        this.setSize(600, 600);
        this.setMinimumSize(new Dimension(400, 510));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Content panel de type BorderPanel --> nord = titre / centre = contenu / sud = licence, droits, auteurs ...
        pContentPanel = new JPanel(new BorderLayout());
        pContentPanel.setBackground(Color.white);
        this.setContentPane(pContentPanel);

        // Titre
        lTitre = new JLabel(new ImageIcon(".\\Ressources\\Bandeau Trocabi v1.jpg"));
        pContentPanel.add(lTitre, BorderLayout.NORTH);

        /*
         * GridBag  panel --> quatre cases
         * chaque case = bouton (image) + label (dans un panel GridBag)
         */
        pCenter = new JPanel(new GridBagLayout());
        GridBagConstraints tmp = new GridBagConstraints();
        // Sous-panel Dépôt
        pDepot = new JPanel(new GridBagLayout());
        jbDepot = new JButton(new ImageIcon(".\\Ressources\\shipping.png"));
        jbDepot.setBackground(new Color(pCenter.getBackground().getRGB()));
        jbDepot.setBorderPainted(false);
        jbDepot.addMouseListener(this);
        jbDepot.addActionListener(this);
        tmp.gridx = 0;
        tmp.gridy = 0;
        pDepot.add(jbDepot, tmp);
        tmp.gridy = 1;
        pDepot.add(new JLabel("<html><body><br/>Dépôt</body></html>"), tmp);
        // Sous-panel Facturation
        pFacturation = new JPanel(new GridBagLayout());
        jbFacturation = new JButton(new ImageIcon(".\\Ressources\\invoice.png"));
        jbFacturation.setBackground(new Color(pCenter.getBackground().getRGB()));
        jbFacturation.setBorderPainted(false);
        jbFacturation.addMouseListener(this);
        jbFacturation.addActionListener(this);
        tmp.gridy = 0;
        pFacturation.add(jbFacturation, tmp);
        tmp.gridy = 1;
        pFacturation.add(new JLabel("<html><body><br/>Facturation</body></html>"), tmp);
        // Sous-panel Vente
        pVente = new JPanel(new GridBagLayout());
        jbVente = new JButton(new ImageIcon(".\\Ressources\\credit-card.png"));
        jbVente.setBackground(new Color(pCenter.getBackground().getRGB()));
        jbVente.setBorderPainted(false);
        jbVente.addMouseListener(this);
        jbVente.addActionListener(this);
        tmp.gridy = 0;
        pVente.add(jbVente, tmp);
        tmp.gridy = 1;
        pVente.add(new JLabel("<html><body><br/>Vente</body></html>"), tmp);
        // Sous-panel Inventaire
        pInventaire = new JPanel(new GridBagLayout());
        jbInventaire = new JButton(new ImageIcon(".\\Ressources\\checklist.png"));
        jbInventaire.setBackground(new Color(pCenter.getBackground().getRGB()));
        jbInventaire.setBorderPainted(false);
        jbInventaire.addMouseListener(this);
        jbInventaire.addActionListener(this);
        tmp.gridy = 0;
        pInventaire.add(jbInventaire, tmp);
        tmp.gridy = 1;
        pInventaire.add(new JLabel("<html><body><br/>Inventaire</body></html>"), tmp);
        // Mise en place des sous-panels
        tmp.weightx = 0.25;
        tmp.weighty = 0.25;
        pCenter.add(pInventaire, tmp);
        tmp.gridx = 1;
        pCenter.add(pFacturation, tmp);
        tmp.gridy = 0;
        pCenter.add(pVente, tmp);
        tmp.gridx = 0;
        pCenter.add(pDepot, tmp);
        pContentPanel.add(pCenter);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if (source == jbDepot) {
            TROCABI.accueil(TROCABI.DEPOT);
        } else if (source == jbVente) {
            TROCABI.accueil(TROCABI.VENTE);
        } else if (source == jbInventaire) {
            TROCABI.accueil(TROCABI.INVENTAIRE);
        } else if (source == jbFacturation) {
            TROCABI.accueil(TROCABI.FACTURATION);
        } else {
            TROCABI.fenetreExterne(TROCABI.ERREUR, "Erreur inattendue : source de l'évènement inconnue");
        }
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        Object source = me.getSource();
        ((JButton) source).setBackground(null);
        ((JButton) source).setBorderPainted(true);
    }

    @Override
    public void mouseExited(MouseEvent me) {
        Object source = me.getSource();
        ((JButton) source).setBackground(new Color(pCenter.getBackground().getRGB()));
        ((JButton) source).setBorderPainted(false);
    }

    @Override
    public void mousePressed(MouseEvent me) {
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseClicked(MouseEvent me) {
    }
}
